package com.prolificinteractive.materialcalendarview.sample.decorators;

import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.DayViewDecorator;
import com.prolificinteractive.materialcalendarview.DayViewFacade;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.ResourceTable;

import java.io.IOException;
import java.util.HashSet;

/**
 * Decorate 2 days.
 *
 * @since 2021-07-14
 */
public class RangeDayDecorator implements DayViewDecorator {

    private final HashSet<CalendarDay> list = new HashSet<>();
    private Element drawable;

    public RangeDayDecorator(final Context context) {
        try {
            Resource bgResource = context.getResourceManager().getResource(ResourceTable.Media_ic_my_selector);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(bgResource, srcOpts);
            PixelMap pixelMap = imageSource.createPixelmap(null);
            drawable = new PixelMapElement(pixelMap);
//            drawable = new StateElement();
//            drawable.setExitFadeDuration(300);
//            drawable.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, checked);
//            drawable.addState(new int[]{ComponentState.COMPONENT_STATE_PRESSED}, checked);
//            ShapeElement empty = new ShapeElement();
//            empty.setRgbColor(RgbColor.fromArgbInt(Color.TRANSPARENT.getValue()));
//            empty.setShape(ShapeElement.OVAL);
//            drawable.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, empty);

//            drawable = context.getResources().getDrawable(R.drawable.my_selector);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean shouldDecorate(CalendarDay day) {
        return list.contains(day);
    }

    @Override
    public void decorate(DayViewFacade view) {
        view.setSelectionDrawable(drawable);
    }

    /**
     * We're changing the dates, so make sure to call {@linkplain MaterialCalendarView#invalidateDecorators()}
     *
     * @param first first
     * @param last last
     */
    public void addFirstAndLast(final CalendarDay first, final CalendarDay last) {
        list.clear();
        list.add(first);
        list.add(last);
    }
}
